﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;



namespace PayRoll
{
    public partial class HolidaysMaster : CustomFormTemplate
    {
        public HolidaysMaster()
        {
            InitializeComponent();
        }
        
        private void HolidaysMaster_Load(object sender, EventArgs e)
        {
            this.Left = 0;
            this.Top = 0;
            this.Tag = "view";
            txtHolidayName.Text = "";

            base.ToolBarButtonClick += new ToolBarButtonClickEventHandler(HolidaysMaster_ToolBarButtonClick);
            base.EnableControls += new EnableControlsEventHandler(HolidaysMaster_EnableControls);
            base.KeyDown += new KeyEventHandler(HolidaysMaster_KeyDown);
        }

        void HolidaysMaster_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (this.Tag.ToString() == "view")
                {
                    HolidaysMaster_ToolBarButtonClick("exit");
                }
                else
                {
                    HolidaysMaster_ToolBarButtonClick("revert");
                }
            }
        }

        void HolidaysMaster_EnableControls(bool Enable)
        {
            fraDtl.Enabled = Enable;
        }

        void HolidaysMaster_ToolBarButtonClick(string Button)
        {
            if (CommonFunction.InList(Button, "add", "edit", "delete") == true)
            {
                if ((Button == "edit" || Button == "delete"))
                {
                    if (txtHolidayName.Text == "")
                    {
                        return;
                    }
                }
            }
            switch (Button)
            {
                case "exit":
                    if (UIFunction.SureToExit("") == true)
                    {
                        ClearError();
                        this.Close();
                    }
                    break;
                case "revert":
                    ClearError();
                    this.Tag = "view";
                    HolidaysMaster_EnableControls(false);
                    this.ToolBarEnterEditMode = false;
                    break;
                case "print":
                    //CrytalEditor newObject = new CrytalEditor();
                    //DataSet dsLocal = new DataSet();
                    //string CompanyDescr = getCompanyData();
                    //CompanyDescr = CompanyDescr + "Raw Material Checklist";
                    //try {
                    //    OpenBlClass();
                    //    dsLocal.Tables.Add(blRM.GetDataTable("Select * From Mst_Authority where Cocd = " + objCompany.Code + " Order by Descr", "mst_Approval"));
                    //    newObject.LoadPreviewer("Rep_Mst_Approval.rpt", dsLocal, enmReportType.FAMasters, CompanyDescr);
                    //} catch {
                    //} finally {
                    //    CloseBlClass();
                    //    newObject = null;
                    //    dsLocal = null;
                    //}
                    break;
                case "find":
                    ClearError();
                    DataSet dsLocal = new DataSet();
                    Popup popup = new Popup();
                    try
                    {
                        ClsHolidaysMaster ObjHolidaysMaster = new ClsHolidaysMaster();
                        dsLocal.Tables.Add(ObjHolidaysMaster.GetAllHolidayNames());
                        dsLocal.Tables[0].TableName = "popup";
                        popup.Width = 30;
                        popup.ShowPopup(null, "HolidaysMasterID", ref dsLocal, "popup", "HolidayName", "", "Manage Holidays", "HolidayName", "20");
                        if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
                        {
                            uiGetData(Convert.ToInt32(popup.Field("HolidaysMasterID")));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        dsLocal = null;
                        popup = null;
                    }
                    break;
                case "add":
                    ClearError();
                    if (uiGetData(0))
                    {
                        this.ToolBarEnterEditMode = true;
                        this.Tag = "add";
                        HolidaysMaster_EnableControls(true);
                        txtHolidayName.Select();
                    }
                    break;
                case "edit":
                    ClearError();
                    this.ToolBarEnterEditMode = true;
                    this.Tag = "edit";
                    HolidaysMaster_EnableControls(true);
                    txtHolidayName.Select();
                    break;
                case "save":
                    if (!ValidateName())
                        return;
                    if (uiPutData() == true & this.Tag.ToString() == "edit")
                    {
                        this.Tag = "view";
                        this.ToolBarEnterEditMode = false;
                        HolidaysMaster_EnableControls(false);
                    }
                    break;
                case "delete":
                    ClearError();
                    uiDeleteData();
                    break;
            }
        }

        private void uiDeleteData()
        {
            try

            {
                ClsHolidaysMaster objHolidaysMaster = new ClsHolidaysMaster();
                objHolidaysMaster.HolidaysMasterID = Convert.ToInt32(txtHolidayName.Tag);
                objHolidaysMaster.DeleteHolidayName();
                uiGetData(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }
        private bool uiPutData()
        {
            bool functionReturnValue = false;
            try
            {
                ClsHolidaysMaster objHolidaysMaster = new ClsHolidaysMaster();
                objHolidaysMaster.HolidayName = txtHolidayName.Text.Trim();
                objHolidaysMaster.HolidaysMasterID = Convert.ToInt32(txtHolidayName.Tag);
                if (objHolidaysMaster.SaveHolidayName(this.Tag.ToString()))
                    functionReturnValue = true;
                uiGetData(0);
                if (this.Tag.ToString() == "add")
                    txtHolidayName.Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool uiGetData(int KeyField)
        {
            bool functionReturnValue = false;
            try
            {
                ClsHolidaysMaster objHolidaysMaster = new ClsHolidaysMaster();
                objHolidaysMaster.GetHolidayName(KeyField);
                txtHolidayName.Tag = objHolidaysMaster.HolidaysMasterID;
                txtHolidayName.Text = objHolidaysMaster.HolidayName;
                functionReturnValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool ValidateName()
        {
            bool bStatus = true;
            if (txtHolidayName.Text == "")
            {
                errorProvider1.SetError(txtHolidayName, "Please enter the Holiday Name");
                return false;
            }
            ClearError();
            return bStatus;
        }
        private void ClearError()
        {
            errorProvider1.Clear();
        }
       
    }
}
